using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ArchSoft
{
    public partial class ContactMail : Form
    {
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();

        private string _projectPath;

        public string ProjectPath
        {
            set { _projectPath = value; }
        }

        public ContactMail()
        {
            InitializeComponent();
            //dt.Columns.Add("Name");
            //dt.Columns.Add("Company");
            //dt.Columns.Add("Telephone");
            //dt.Columns.Add("Email");

        }

        public string getEmail()
        {
            return email;
        }

        public string getName()
        {
            return name;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                AddContactMail frm = new AddContactMail();
                frm.ProjectPath = _projectPath;
                frm.ShowDialog();

                listView1.Items.Clear();
                dt.Rows.Clear();
                //string path = @"D:\SideDB\MailApp\Contact.scr";
                string path = _projectPath + Path.DirectorySeparatorChar + "Contact.scr";
                if (File.Exists(path))
                {
                    ds.ReadXml(path);
                    dt = ds.Tables[0];

                    DataRow row = dt.NewRow();
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        ListViewItem item = new ListViewItem();
                        ListViewItem.ListViewSubItem item1 = new ListViewItem.ListViewSubItem();
                        ListViewItem.ListViewSubItem item2 = new ListViewItem.ListViewSubItem();
                        ListViewItem.ListViewSubItem item3 = new ListViewItem.ListViewSubItem();
                        ListViewItem.ListViewSubItem item4 = new ListViewItem.ListViewSubItem();

                        item.Text = dt.Rows[i]["Name"].ToString();
                        item2.Text = dt.Rows[i]["Company"].ToString();
                        item3.Text = dt.Rows[i]["Telephone"].ToString();
                        item4.Text = dt.Rows[i]["Email"].ToString();

                        item.SubItems.AddRange(new ListViewItem.ListViewSubItem[] { item2, item3, item4 });
                        listView1.Items.Add(item);
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            string path = _projectPath + Path.DirectorySeparatorChar + "Contact.scr";
            //string path = @"D:\SideDB\MailApp\Contact.scr";
            listView1.Items.Clear();

            if (File.Exists(path))
            {
                ds.ReadXml(path);
                dt = ds.Tables[0];

                DataRow row = dt.NewRow();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ListViewItem item = new ListViewItem();
                    ListViewItem.ListViewSubItem item1 = new ListViewItem.ListViewSubItem();
                    ListViewItem.ListViewSubItem item2 = new ListViewItem.ListViewSubItem();
                    ListViewItem.ListViewSubItem item3 = new ListViewItem.ListViewSubItem();
                    ListViewItem.ListViewSubItem item4 = new ListViewItem.ListViewSubItem();

                    item.Text = dt.Rows[i]["Name"].ToString();
                    item2.Text = dt.Rows[i]["Company"].ToString();
                    item3.Text = dt.Rows[i]["Telephone"].ToString();
                    item4.Text = dt.Rows[i]["Email"].ToString();

                    item.SubItems.AddRange(new ListViewItem.ListViewSubItem[] { item2, item3, item4 });
                    listView1.Items.Add(item);
                }
            }
        }


        string email;
        string name;
        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                ListViewItem item = listView1.SelectedItems[0];
                email = item.SubItems[3].Text;
                name = item.SubItems[0].Text;
                this.Close();
            }
            catch (Exception ex)
            {

            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                ListViewItem item = listView1.SelectedItems[0];
                string a = item.SubItems[3].Text;
                string path = _projectPath + Path.DirectorySeparatorChar + "Contact.scr";
                //string path = @"D:\SideDB\MailApp\Contact.scr";
                dt.Rows.Clear();
                if (File.Exists(path))
                {
                    ds.ReadXml(path);
                    dt = ds.Tables[0];
                }

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string name = dt.Rows[i]["Email"].ToString();
                    if (a == name)
                    {
                        dt.Rows[i].Delete();
                    }
                }

                DataTable dt1 = dt.Copy();

                DataSet ds1 = new DataSet();
                ds1.Tables.Add(dt1);
                ds1.WriteXml(path);

                ViewList();
            }
            catch (Exception ex)
            {

            }
        }

        public void ViewList()
        {
            string path = _projectPath + Path.DirectorySeparatorChar + "Contact.scr";
            //string path = @"D:\SideDB\MailApp\Contact.scr";
            listView1.Items.Clear();
            dt.Rows.Clear();
            if (File.Exists(path))
            {
                ds.ReadXml(path);
                dt = ds.Tables[0];

                DataRow row = dt.NewRow();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ListViewItem item = new ListViewItem();
                    ListViewItem.ListViewSubItem item1 = new ListViewItem.ListViewSubItem();
                    ListViewItem.ListViewSubItem item2 = new ListViewItem.ListViewSubItem();
                    ListViewItem.ListViewSubItem item3 = new ListViewItem.ListViewSubItem();
                    ListViewItem.ListViewSubItem item4 = new ListViewItem.ListViewSubItem();

                    item.Text = dt.Rows[i]["Name"].ToString();
                    item2.Text = dt.Rows[i]["Company"].ToString();
                    item3.Text = dt.Rows[i]["Telephone"].ToString();
                    item4.Text = dt.Rows[i]["Email"].ToString();

                    item.SubItems.AddRange(new ListViewItem.ListViewSubItem[] { item2, item3, item4 });
                    listView1.Items.Add(item);
                }
            }
        }


        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
           
        }

        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                AddContactMail frm = new AddContactMail();
                frm.ProjectPath = _projectPath;
                ListViewItem item = listView1.SelectedItems[0];

                string email = item.SubItems[3].Text;
                string name = item.SubItems[0].Text;
                string company = item.SubItems[1].Text;
                string telephone = item.SubItems[2].Text;

                frm.NameHHH = name;
                frm.Email = email;
                frm.Company = company;
                frm.Telephone = telephone;
                frm.Modify = true;
                frm.ShowDialog();

                ViewList();
            }
            catch (Exception ex)
            {
                
            }
        }
    }
}